package com.ecnu.jiagou.damai.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.ecnu.jiagou.damai.entity.Performance;
import com.ecnu.jiagou.damai.entity.Show;
import com.ecnu.jiagou.damai.result.Result;
import com.ecnu.jiagou.damai.service.ShowService;
import com.ecnu.jiagou.damai.utils.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/show")
public class ShowController {

    @Autowired
    private ShowService showService;

    @Autowired
    private RedisUtil redisUtil;
    /**
     * 根据演出id获取演出的具体信息
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Show> getShow(@RequestParam("id") Long id){
        //System.out.println("========"+id);
        Show show = showService.getShow(id);
        return Result.success(show);
    }

    /**
     * 分页展示所有演出信息
     * @param request
     * @return
     */
    @PostMapping("/query")
    public Result<PageInfo<Performance>> getAll(HttpServletRequest request){
        int page = Integer.parseInt(request.getParameter("page"));
        int size = Integer.parseInt(request.getParameter("size"));
        
        PageHelper.startPage(page, size);
        List<Performance> shows = showService.getAllShows();

        PageInfo<Performance> pageInfo = new PageInfo<>(shows);
        return Result.success(pageInfo);
    }

    /**
     * 分页查询演出信息
     * @param city
     * @param type
     * @param start
     * @param end
     * @param words
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/all")
    public Result<PageInfo<Performance>> queryByFactor(String city, Integer type, String start, String end, String words, int page, int size){

        PageHelper.startPage(page, size);
        List<Performance> list = showService.queryShows(city, type, start, end, words);
        PageInfo<Performance> pageInfo = new PageInfo<>(list);
        redisUtil.set("Performance_total_Size",pageInfo.getTotal());
        return Result.success(pageInfo);
    }

    /**
     * 猜你喜欢，根据浏览的演出类型查询最近的4场演出
     * @param page
     * @return
     */
    @GetMapping("/like")
    public Result<List<Show>> getYouLike(Integer page){
        return showService.getShowByLike(page);
    }

    @GetMapping("/ticketInfo")
    public Result<Performance> getDetail(Integer ticketid){
        return showService.getDetail(ticketid);
    }
}
